package inf3791.calendarsystem.others;

import java.awt.GridBagConstraints;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.swing.JButton;

/**
 * Displays a simple menu.
 *
 * @author Jonas Weber
 */
public class OthersMainMenu extends javax.swing.JPanel {

    public enum MenuEntrySets {

        Login,
        MedicalStaff,
        Family
    }
    private final Map<MenuEntrySets, List<JButton>> mappings = new HashMap<>();

    static {
    }
    private MenuEntrySets set = MenuEntrySets.Login;

    public final MenuEntrySets getSet() {
        return set;
    }

    public final void setSet(MenuEntrySets set) {
        this.set = set;

        removeAll();
        updateUI();
        if (mappings.containsKey(set) && mappings.get(set) != null) {
            for (JButton jb : mappings.get(set)) {
                GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.gridx = 0;
                gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
                gridBagConstraints.weightx = 1.0;

                add(jb, gridBagConstraints);
            }
            updateUI();
        }

    }

    public class MainMenuClickedEvent extends java.util.EventObject {

        public MainMenuClickedEvent(Object source) {
            super(source);
        }
    }

    public interface MainMenuClickedListener extends java.util.EventListener {

        public void home(MainMenuClickedEvent evt);

        public void calendar(MainMenuClickedEvent evt);

        public void appointments(MainMenuClickedEvent evt);

        public void addnewevent(MainMenuClickedEvent evt);
        
        public void patients(MainMenuClickedEvent evt);
    }
    List<MainMenuClickedListener> menulisteners = new LinkedList<>();

    public void addMenuClickedListener(MainMenuClickedListener listener) {
        menulisteners.add(listener);
    }

    public void removeMenuClickedListener(MainMenuClickedListener listener) {
        menulisteners.remove(listener);
    }

    /**
     * Creates new form OthersMainMenu
     */
    public OthersMainMenu() {
        initComponents();

        mappings.put(MenuEntrySets.Login, null);
        mappings.put(MenuEntrySets.MedicalStaff, Arrays.asList(buthome, butcalendar, butpatients, butnewevent));
        mappings.put(MenuEntrySets.Family, Arrays.asList(buthome, butcalendar, butnewevent));

        setSet(MenuEntrySets.Login);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buthome = new javax.swing.JButton();
        butnewevent = new javax.swing.JButton();
        butcalendar = new javax.swing.JButton();
        butpatients = new javax.swing.JButton();

        buthome.setText("Home");
        buthome.setMaximumSize(null);
        buthome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buthomeActionPerformed(evt);
            }
        });

        butnewevent.setText("Add New Event");
        butnewevent.setMaximumSize(null);
        butnewevent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butneweventActionPerformed(evt);
            }
        });

        butcalendar.setText("Calendar");
        butcalendar.setMaximumSize(null);
        butcalendar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butcalendarActionPerformed(evt);
            }
        });

        butpatients.setText("Patients");
        butpatients.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butpatientsActionPerformed(evt);
            }
        });

        java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
        layout.rowHeights = new int[] {30};
        setLayout(layout);
    }// </editor-fold>//GEN-END:initComponents

    private void buthomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buthomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_buthomeActionPerformed

    private void butcalendarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butcalendarActionPerformed
        for (MainMenuClickedListener l : menulisteners) {
            l.calendar(new MainMenuClickedEvent(this));
        }
    }//GEN-LAST:event_butcalendarActionPerformed

    private void butneweventActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butneweventActionPerformed
        for (MainMenuClickedListener l : menulisteners) {
            l.addnewevent(new MainMenuClickedEvent(this));
        }
    }//GEN-LAST:event_butneweventActionPerformed

    private void butpatientsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butpatientsActionPerformed
        for (MainMenuClickedListener l : menulisteners) {
            l.patients(new MainMenuClickedEvent(this));
        }
    }//GEN-LAST:event_butpatientsActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butcalendar;
    private javax.swing.JButton buthome;
    private javax.swing.JButton butnewevent;
    private javax.swing.JButton butpatients;
    // End of variables declaration//GEN-END:variables
}
